<template>
    <div class="box">
    <div class="main-box">
        <div class="container">
            <form>
                <div style="display: flex;justify-content: center;align-items: center">
                    <span style="font-size: 27px;font-weight: bolder;color: black">基于知识图谱的制造产业链全景信息系统</span>

                    <el-image style="width: 60px;height: 60px;padding-left: 10px" :src="require('../../assets/img/relation.png')" ></el-image>
                </div>
                <el-tabs stretch v-model="activeName">
                    <el-tab-pane label="登录" name="login">
                        <div style="display: flex;flex-direction: column;justify-content: center;align-items: center">
                            <input class="form__input" type="text" placeholder="请输入用户名" v-model="loginForm.username" />
                            <input class="form__input" type="password" placeholder="请输入登录密码" v-model="loginForm.password" />
                            <div class="primary-btn" @click="login">立即登录</div>
                        </div>
                    </el-tab-pane>
                    <el-tab-pane  label="注册" name="register">
                        <div style="display: flex;flex-direction: column;justify-content: center;align-items: center">
                            <input class="form__input" type="text" placeholder="请输入您的用户名" v-model="registerForm.username" />
                            <input class="form__input" type="text" placeholder="请输入您的手机号" v-model="registerForm.phone" />
                            <input class="form__input" type="password" placeholder="请输入您的密码" v-model="registerForm.password" />
                            <input class="form__input" type="password" placeholder="请确认您的密码" v-model="passwordAgain"/>
                            <div class="primary-btn" @click="register">立即注册</div>
                        </div>
                    </el-tab-pane>

                </el-tabs>
            </form>
        </div>
    </div>
    </div>
</template>

<script>

import {getUserInfo, login, register} from "@/api/user";

export default {
    name: 'Index',
    data() {
        return {
            isLogin: true,
            loginForm: {
                username: '',
                password: '',
            },
            registerForm: {
                username: '',
                password: '',
                phone:''
            },
            passwordAgain:"",
            activeName:"login"
        }
    },
    methods: {
       async login(){
            if (this.loginForm.password === "" || this.loginForm.username ===""){
                this.$message({
                    type:"warning",
                    message:"用户名/密码未输入"
                })
                return
            }
           await login(this.loginForm).then((response)=>{
               if (response.data.code !== 200){
                   this.$message({
                       type:"warning",
                       message:"登录失败"
                   })
                   return
               }
               let data=response.data.data
               window.localStorage.setItem("token",data.tokenName)
               console.log(data)
           }).catch((err)=>{
               console.log(err)
           })
           let token = window.localStorage.getItem("token")
           if (token ===""){
               this.$message({
                   type:"warning",
                   message:"请先登录"
               })
               this.$router.push("/login")
           }
           await getUserInfo({accessToken:token}).then((response)=>{
               if (response.data.code !== 200){
                   this.$message({
                       type:"warning",
                       message:"登录失败"
                   })
                   return
               }
               let data= response.data.data
               this.$store.commit("setUserInfoAvatar",data.avatar)
               this.$store.commit("setUserInfoUsername",data.username)
               this.$message.success("登录成功!")
               this.$router.push("/g/twodgraph")
           })
        },
        async register(){
            if (this.registerForm.username==="" || this.registerForm.password === "" || this.passwordAgain==="" || this.registerForm.phone === ""){
                this.$message({
                    type:"warning",
                    message:"用户名/手机号/密码/确认密码未输入"
                })
                return
            }
            if (this.registerForm.password !== this.passwordAgain){
                this.$message({
                    type:"warning",
                    message:"两次密码输入不一致"
                })
                this.registerForm.password=""
                this.passwordAgain=""
                return;
            }
            // 进行注册
            await register(this.registerForm).then((response)=>{
                if (response.data.code !== 200){
                    this.$message({
                        type:"warning",
                        message:"注册失败"
                    })
                    return
                }else {
                    this.$message.success("注册成功!")
                }
            })
            this.
            this.loginForm.username=this.registerForm.username
            this.loginForm.password=this.registerForm.password
            await this.login()
        },

    },
}
</script>

<style lang="scss" scoped>
.box{
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background: url('../../assets/img/industry.jpg') no-repeat ;
    background-size: cover;
}
.main-box {
    width: 100%;
    height: 100%;
  .container {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 0;right: 0;
    width: 600px;
    padding: 25px;
      height: 100%;
    background-color: #ecf0f3;
    transition: all 1.25s;
    form {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      width: 100%;
      height: 100%;
      color: #a0a5a8;
      .title {
        font-size: 34px;
        font-weight: 700;
        line-height: 3;
        color: #181818;
      }
      .text {
        margin-top: 30px;
        margin-bottom: 12px;
      }
      .form__input {
        width: 350px;
        height: 40px;
        margin: 4px 0;
        padding-left: 25px;
        font-size: 16px;
        letter-spacing: 0.15px;
        border: none;
        outline: none;
        // font-family: 'Montserrat', sans-serif;
        background-color: #ecf0f3;
        transition: 0.25s ease;
        border-radius: 8px;
        box-shadow: inset 2px 2px 4px #d1d9e6, inset -2px -2px 4px #f9f9f9;
        &::placeholder {
          color: #a0a5a8;
        }
      }
    }
  }
  .container-register {
    z-index: 100;
    left: calc(100% - 600px);
  }
  .container-login {
    left: calc(100% - 600px);
    z-index: 0;
  }
  .is-txl {
    left: 0;
    transition: 1.25s;
    transform-origin: right;
  }
  .is-z200 {
    z-index: 200;
    transition: 1.25s;
  }
  .switch {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 400px;
    padding: 50px;
    z-index: 200;
    transition: 1.25s;
    background-color: #ecf0f3;
    overflow: hidden;
    box-shadow: 4px 4px 10px #d1d9e6, -4px -4px 10px #f9f9f9;
    color: #a0a5a8;
    .switch__circle {
      position: absolute;
      width: 500px;
      height: 500px;
      border-radius: 50%;
      background-color: #ecf0f3;
      box-shadow: inset 8px 8px 12px #d1d9e6, inset -8px -8px 12px #f9f9f9;
      bottom: -60%;
      left: -60%;
      transition: 1.25s;
    }
    .switch__circle_top {
      top: -30%;
      left: 60%;
      width: 300px;
      height: 300px;
    }
    .switch__container {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      position: absolute;
      width: 400px;
      padding: 50px 55px;
      transition: 1.25s;
      h2 {
        font-size: 34px;
        font-weight: 700;
        line-height: 3;
        color: #181818;
      }
      p {
        font-size: 14px;
        letter-spacing: 0.25px;
        text-align: center;
        line-height: 1.6;
      }
    }
  }
  .login {
    left: calc(100% - 400px);
    .switch__circle {
      left: 0;
    }
  }
  .primary-btn {
    width: 180px;
    height: 50px;
    border-radius: 25px;
    margin-top: 50px;
    text-align: center;
    line-height: 50px;
    font-size: 14px;
    letter-spacing: 2px;
    background-color: #4b70e2;
    color: #f9f9f9;
    cursor: pointer;
    box-shadow: 8px 8px 16px #d1d9e6, -8px -8px 16px #f9f9f9;
    &:hover {
      box-shadow: 4px 4px 6px 0 rgb(255 255 255 / 50%),
      -4px -4px 6px 0 rgb(116 125 136 / 50%),
      inset -4px -4px 6px 0 rgb(255 255 255 / 20%),
      inset 4px 4px 6px 0 rgb(0 0 0 / 40%);
    }
  }
}
.graph-image{
    padding: 0px !important;
    background-color: #f0f0f0 !important;
}
::v-deep .el-tabs__item{
    font-size: 20px;
}
</style>